package com.li.network.socket.TCP;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author li
 * @date 2023/6/14 12:13
 */
public class Server {
    public static void main(String[] args) throws IOException {
        int port = 8686;
        //欢迎套接字：做好准备，接受客户端请求
        try (ServerSocket server = new ServerSocket(port)) {
            //连接套接字
            Socket socket = server.accept();

            //读请求
            Reader reader = new InputStreamReader(socket.getInputStream());
            char[] chars = new char[1024];
            StringBuilder builder = new StringBuilder();
            while (reader.read(chars) != -1) {
                builder.append(new String(chars, 0, chars.length));
            }
            System.out.println("Receive from client message = " + builder);

            //写响应
            OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
            char[] data = builder.toString().toUpperCase().toCharArray();
            writer.write(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
